package st.business.tepth.business.taskallocation.lag;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.LagInfoBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-06-10 14:31
 */

public class LagModel implements LagContract.Model {

    @Override
    public Disposable getLagInfo(String token, int repairId,
                                 final OnDataConvertListener<List<LagInfoBean>> listener) {
        return RxRestClient.builder()
                .url(Url.GetLagWorkInfo)
                .params("token", token)
                .params("repairId", repairId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertToLagInfo(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    private List<LagInfoBean> convertToLagInfo(String response) {
        return new ObjConverter<List<LagInfoBean>>().convert(response
                , new TypeReference<List<LagInfoBean>>() {
                });
    }
}
